namespace ScrumSprintMonitor.Implementation.AvailableTimeCalculator
{
	#region #using Directives

	using System;
	using System.Diagnostics;

	using Infrastructure;

	#endregion

	public static class DateTimeExtensions
	{
		public static DateTime ToTeamMemberTimeZone(this DateTime dateTime, ITeamMemberInformation teamMemberInfo)
		{
			if (dateTime.Kind == DateTimeKind.Unspecified)
			{
				return dateTime; // It is already in the target timezone
			}

			var localizedDateTime = TimeZoneInfo.ConvertTimeFromUtc(dateTime.ToUniversalTime(), teamMemberInfo.TimeZone);

			// When the output teamMemberInfo.TimeZone is also UTC, then no translation occurs. Therefore we need to mark Kind manually.
			if (localizedDateTime.Kind != DateTimeKind.Unspecified)
			{
				Debug.Assert(localizedDateTime.Kind == DateTimeKind.Utc);

				localizedDateTime = new DateTime(localizedDateTime.Ticks, DateTimeKind.Unspecified);
			}

			return localizedDateTime;
		}
	}
}